package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import com.ouheng.common.config.Constant;
import com.ouheng.shop.dao.RegionDao;
import com.ouheng.shop.domain.RegionDO;
import com.ouheng.shop.service.RegionService;



@Service
public class RegionServiceImpl implements RegionService {
	@Autowired
	private RegionDao regionDao;
	
	@Override
	public RegionDO get(Long id){
		return regionDao.get(id);
	}
	
	@Override
	public List<RegionDO> list(Map<String, Object> map){
		return regionDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return regionDao.count(map);
	}
	
	@Override
	public int save(RegionDO region){
		return regionDao.save(region);
	}
	
	@Override
	public int update(RegionDO region){
		return regionDao.update(region);
	}
	
	@Override
	public int remove(Long id){
		return regionDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return regionDao.batchRemove(ids);
	}

	@Override
	@Transactional
	public void changeStatus(Long id, String cmd) {
		RegionDO  regionDO  = regionDao.get(id);
		if(regionDO==null){
			return;
		}
		if(Constant.STATUS_RUNNING_STOP.equals(cmd)){
			if("1".equals(regionDO.getLevel())){
				List<RegionDO> listLevelTow = regionDao.listByPid(regionDO.getId());
				for (RegionDO regionDO2 : listLevelTow) {
					List<RegionDO> listLevelThree = regionDao.listByPid(regionDO2.getId());
					for (RegionDO regionDO3 : listLevelThree) {
						regionDO3.setStatus("1");
						regionDao.update(regionDO3);
					}
					regionDO2.setStatus("1");
					regionDao.update(regionDO2);
				}
				regionDO.setStatus("1");
				regionDao.update(regionDO);
			}else if("2".equals(regionDO.getLevel())){
				List<RegionDO> listLevelThree = regionDao.listByPid(regionDO.getId());
				for (RegionDO regionDO2 : listLevelThree) {
					regionDO2.setStatus("1");
					regionDao.update(regionDO2);
				}
				regionDO.setStatus("1");
				regionDao.update(regionDO);
			}else{
				regionDO.setStatus("1");
				regionDao.update(regionDO);
			}
			
		}else{
			if("1".equals(regionDO.getLevel())){
				List<RegionDO> listLevelTow = regionDao.listByPid(regionDO.getId());
				for (RegionDO regionDO2 : listLevelTow) {
					List<RegionDO> listLevelThree = regionDao.listByPid(regionDO2.getId());
					for (RegionDO regionDO3 : listLevelThree) {
						regionDO3.setStatus("2");
						regionDao.update(regionDO3);
					}
					regionDO2.setStatus("2");
					regionDao.update(regionDO2);
				}
				regionDO.setStatus("2");
				regionDao.update(regionDO);
			}else if("2".equals(regionDO.getLevel())){
				List<RegionDO> listLevelThree = regionDao.listByPid(regionDO.getId());
				for (RegionDO regionDO2 : listLevelThree) {
					regionDO2.setStatus("2");
					regionDao.update(regionDO2);
				}
				regionDO.setStatus("2");
				regionDao.update(regionDO);
			}else{
				regionDO.setStatus("2");
				regionDao.update(regionDO);
			}
		}
	}
	
}
